package com.yun.datastructure.linear.list.linkedlist;

import java.util.Iterator;
import java.util.function.Consumer;

/**
 * 双向链表(带哨兵)
 */
public class DoublyLinkedListSentinel implements Iterable<Integer> {
    static class Node {
        Node prev;
        int value;
        Node next;

        public Node(Node prev, int value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }

    private Node head;
    private Node tail;

    public DoublyLinkedListSentinel(){
        head = new Node(null,-100,null);
        tail = new Node(null,100,null);
        head.next=tail;
        tail.prev=head;
    }

    private Node findNode(int index){
        int i = -1;
        for (Node p = head;p !=tail;p=p.next,i++){
            if(i == index){
                return p;
            }
        }
        return null;
    }

    public void addFirst(int value){
        insert(0,value);
    }

    public void removeFirst(){
        remove(0);
    }

    public void addLast(int value){
        Node last =tail.prev;
        Node added=new Node(last,value,tail);
        last.next=added;
        tail.prev=added;
    }

    public void removeLast(){
        Node removed =tail.prev;
        if(removed == head){
            throw IllegaIndex(0);
        }
        Node prev = removed.prev;
        prev.next=tail;
        tail.prev=prev;
    }

    public void insert(int index,int value){
        Node prev = findNode(index -1);
        if (prev == null){
            throw IllegaIndex(index);
        }
        Node next =prev.next;
        Node inserted = new Node(prev,value,next);
        prev.next=inserted;
        next.prev=inserted;
    }

    public void remove(int index){
        Node prev = findNode(index-1);
        if (prev == null){
            throw IllegaIndex(index);
        }
        Node removed=prev.next;
        if(removed == tail){
            throw IllegaIndex(index);
        }
        Node next =removed.next;

        prev.next=next;
        next.prev=prev;
    }

    public int get(int index){
        Node node =findNode(index);
        if (node == null || node == head || node ==tail) {
            throw IllegaIndex(index);
        }
        return node.value;
    }

    private IllegalArgumentException IllegaIndex(int index) {
        return new IllegalArgumentException(
                String.format("index [%d] 不合法%n", index));
    }

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p =head.next;
            @Override
            public boolean hasNext() {
                return p != tail;
            }

            @Override
            public Integer next() {
                int value = p.value;
                p = p.next;
                return value;
            }
        };
    }

    public void loop(Consumer<Integer> consumer){
        Node p =head.next;
        while (p!=tail){
            consumer.accept(p.value);
            p=p.next;
        }
    }

}
